package ca.etsmtl.log430.lab3.presentation.cmdline;
import java.util.Collection;
import java.util.Iterator;



import ca.etsmtl.log430.lab3.gestion.TeacherAssignmentException;
import ca.etsmtl.log430.lab3.gestion.ifaces.*;
import ca.etsmtl.log430.lab3.presentation.IDisplay;
public class Displays implements IDisplay {

	private int lineCount = 0;
	private int maxLinesDisplayed = 18;
	
	private ITeacherAssignment controller;

	
	
	/**
	 * 
	 * @param controller
	 */
	public Displays(ITeacherAssignment controller) {
		super();
		this.controller = controller;
	}

	/**
	 * Counts the number of lines that has been printed. One a set number of
	 * lines has been printed, the user is asked to press the enter key to
	 * continue. This prevents lines of text from scrolling off of the page
	 * 
	 * @param linesToAdd
	 */
	private void lineCheck(int linesToAdd) {

		Termio terminal = new Termio();

		if (lineCount >= maxLinesDisplayed) {

			lineCount = 0;
			System.out.print("\n*** Press Enter To Continue ***");
			terminal.keyboardReadChar();

		} else {

			lineCount += linesToAdd;

		} // if

	} // LineCheck

	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.presentation.IDisplay#displayTeacher(ca.etsmtl.log430.lab2.gestion.ifaces.ITeacher)
	 */
	@Override
	public void displayTeacher(ITeacher teacher) {

		System.out.print(teacher.getTeacherID() + " " + teacher.getFirstName()
				+ " ");
		System.out.println(teacher.getLastName() + " " + teacher.getTeacherType());

	} // displayTeacher

	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.presentation.IDisplay#displayCourse(ca.etsmtl.log430.lab2.gestion.ifaces.ICourse)
	 */
	@Override
	public void displayCourse(ICourse course) {

		System.out.print(course.getCourseID() + " " + course.getGroup() + " "
				+ course.getCourseName() + " " + course.getType() + " "
				+ course.getDay() + " ");
		
		System.out.println(course.getStartTime() + ":" + course.getStopTime());

	} // DisplayCourse

	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.presentation.IDisplay#displayTeachersAssignedToCourse(ca.etsmtl.log430.lab2.gestion.ifaces.ICourse)
	 */
	@Override
	public void displayTeachersAssignedToCourse(ICourse course) {

		boolean done;
		ITeacher teacher;

		System.out.println("\nTeachers assigned to: " + " ");
		lineCheck(2);
		
		displayCourse(course);
		
		lineCheck(1);

		System.out
				.println("===========================================================");
		lineCheck(1);

		course.getTeachers().goToFrontOfList();
		done = false;

		while (!done) {

			teacher = course.getTeachers().getNextTeacher();

			if (teacher == null) {

				done = true;

			} else {

				displayTeacher(teacher);

			} // if

		} // while

	} // DisplayStudentsRegistered

	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.presentation.IDisplay#displayCoursesAssignedToTeacher(ca.etsmtl.log430.lab2.gestion.ifaces.ITeacher)
	 */
	@Override
	public void displayCoursesAssignedToTeacher(ITeacher teacher) {


		System.out.print("\nCourses assigned (this term) to : "
				+ teacher.getFirstName() + " ");
		System.out
				.println(teacher.getLastName() + " " + teacher.getTeacherID());
		lineCheck(2);
		System.out
				.println("========================================================= ");
		lineCheck(1);

		Collection<ICourse> assignedCourses = teacher.getCoursesAssigned();
		for (ICourse course : assignedCourses) {
			displayCourse(course);
			lineCheck(2);
		}
		
	

	} // displayCoursesAssignedToTeacher

	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.presentation.IDisplay#displayTeacherList(java.util.Collection)
	 */
	@Override
	public void displayTeacherList(Collection<? extends ITeacher> list) {

		ITeacher teacher;

		System.out.print("\n");
		lineCheck(1);
        Iterator<? extends ITeacher> teacherIterator=list.iterator();

		while (teacherIterator.hasNext()) {

			teacher = teacherIterator.next();

			displayTeacher(teacher);
			lineCheck(1);

		} // while

	} // DisplayStudentList

	
	
	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.presentation.IDisplay#displayCourseList(java.util.Collection)
	 */
	@Override
	public void displayCourseList(Collection<? extends ICourse> list) {

		ICourse course;
		System.out.print("\n");
		lineCheck(1);

		Iterator<? extends ICourse> courseIterator = list.iterator();

		while (courseIterator.hasNext()) {

			course = courseIterator.next();
			displayCourse(course);
			lineCheck(1);

		} 

	} // DisplayCourseList

	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.presentation.IDisplay#displayException(ca.etsmtl.log430.lab2.gestion.TeacherAssignmentException)
	 */
	@Override
	public void displayException(TeacherAssignmentException e) {
		System.err.println(e.getMessage());
		
	}

	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.presentation.IDisplay#displaySigleList(java.util.Collection)
	 */
	@Override
	public void displaySigleList(Collection<String> courses) {
		for (String string : courses) {
			System.out.println(string);
		}
		
	}

} // Display